package topic.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import topic.activity.DeliverComment;
import topic.bean.SecondCommentBean;
import topic.bean.TopicDetailBean;
import personalPage.activity.PersonalPage;
import view.MyImageLoader;
import utils.MyLogUtils;
import utils.ToastUtils;
import utils.VariousUtils;
import view.MyClickableSpan;
import view.MyConfirmDialog;

/**
 * Created by Administrator on 2017/3/25.
 * 二级评论列表
 */

public class SecondCommentAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<SecondCommentBean.CommentBean.RecommentListBean> arrayList;
    private String blogid;
    private final int DELETE = 1;
    private static final String TAG = "SecondCommentAdapter";
    public SecondCommentAdapter(Context context, String blogid, ArrayList<SecondCommentBean.CommentBean.RecommentListBean> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
        this.blogid = blogid;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyHolder myHolder;
        if (convertView == null) {
            myHolder = new MyHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.secondcomment_item, null);
            myHolder.second_comment_item_headLayout = (RelativeLayout) convertView.findViewById(R.id.second_comment_item_headLayout);
            myHolder.roundedImageView = (RoundedImageView) convertView.findViewById(R.id.second_comment_item_head_ima);
            myHolder.second_comment_item_name = (TextView) convertView.findViewById(R.id.second_comment_item_name);
            myHolder.second_comment_item_time = (TextView) convertView.findViewById(R.id.second_comment_item_time);
            myHolder.second_comment_item_content = (TextView) convertView.findViewById(R.id.second_comment_item_content);
            myHolder.second_comment_delete = (TextView) convertView.findViewById(R.id.second_comment_delete);
            convertView.setTag(myHolder);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case Constant.ERROR_CODE_SUCCESS:
                        if (msg.arg1 == DELETE) {
                            //删除成功
                            MyLogUtils.e(TAG, "删除成功");
                            arrayList.remove(msg.arg2);
                            SecondCommentAdapter.this.notifyDataSetChanged();
                        } else {
                        }
                        break;
                    default:
                        break;

                }
            }
        };

        MyImageLoader.showImage(arrayList.get(position).getHeadimgurl(), myHolder.roundedImageView);
        myHolder.second_comment_item_name.setText(arrayList.get(position).getNickname());
        myHolder.second_comment_item_time.setText(arrayList.get(position).getCreate_time());
        if (0 == arrayList.get(position).getShowdelete()) {
            //隐藏删除
            myHolder.second_comment_delete.setVisibility(View.GONE);
        } else {
            //显示删除
            myHolder.second_comment_delete.setVisibility(View.VISIBLE);
            myHolder.second_comment_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final MyConfirmDialog myConfirmDialog = new MyConfirmDialog(context, context.getResources().getString(R.string.delete_title));
                    myConfirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
                        @Override
                        public void confirm() {
                            deleteComment(arrayList.get(position).getComment_id(), handler, position);
                        }
                    });
                    myConfirmDialog.show();
                }
            });
        }

        if (TextUtils.isEmpty(arrayList.get(position).getCommentedid())) {
            //直接回复
            myHolder.second_comment_item_content.setText(arrayList.get(position).getContent());
        } else {
            //回复某人
            final SpannableString content = new SpannableString("回复" + arrayList.get(position).getCommentednickname() + ":" + arrayList.get(position).getContent());
            ClickableSpan myClickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Intent i = new Intent(context,PersonalPage.class);
                    i.putExtra("userId",arrayList.get(position).getCommentedid());
                    context.startActivity(i);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    int color = context.getResources().getColor(R.color.common_color);
                    ds.setColor(color);
                    ds.setUnderlineText(false); //去掉下划线
                }
            };
            content.setSpan(myClickableSpan, 2, arrayList.get(position).getCommentednickname().length() + 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            myHolder.second_comment_item_content.setText(content);
            myHolder.second_comment_item_content.setMovementMethod(LinkMovementMethod.getInstance());
        }
        //进入个人主页
        myHolder.second_comment_item_headLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, PersonalPage.class);
                i.putExtra("userId", arrayList.get(position).getUser_id());
                context.startActivity(i);
            }
        });

        //点击文本非名字部分
        myHolder.second_comment_item_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((BaseActivity)context).hasLoad()) {
                    if (!VariousUtils.isSamId(arrayList.get(position).getUser_id())) {
                        //可以评论
                        Intent i = new Intent(context, DeliverComment.class);
                        i.putExtra("blogid", blogid);
                        i.putExtra("commentid", arrayList.get(position).getTo_commentid());
                        i.putExtra("commenterid", arrayList.get(position).getUser_id());
                        i.putExtra("nickname", arrayList.get(position).getNickname());
                        context.startActivity(i);
                    } else {
                        ToastUtils.shortToast("不能评论自己");
                    }
                }else{
                    ((BaseActivity)context).login(context);
                }
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((BaseActivity)context).hasLoad()) {
                    if (!VariousUtils.isSamId(arrayList.get(position).getUser_id())) {
                        //可以评论
                        Intent i = new Intent(context, DeliverComment.class);
                        i.putExtra("blogid", blogid);
                        i.putExtra("commentid", arrayList.get(position).getTo_commentid());
                        i.putExtra("commenterid", arrayList.get(position).getUser_id());
                        i.putExtra("nickname", arrayList.get(position).getNickname());
                        context.startActivity(i);
                    } else {
                        ToastUtils.shortToast("不能评论自己");
                    }
                }else{
                    ((BaseActivity)context).login(context);
                }
            }
        });

        return convertView;
    }

    private void deleteComment(String commentId, Handler handler, int position) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("comment_id", commentId);
        new https.InterNetController(context, Constant.DELETECOMMENT, handler, hashMap, DELETE, position,false);
    }

    class MyHolder {
        RelativeLayout second_comment_item_headLayout;
        RoundedImageView roundedImageView;
        TextView second_comment_item_name;
        TextView second_comment_item_time;
        TextView second_comment_item_content;
        TextView second_comment_delete;
    }
}
